<template>
  <view class="mine">
    <!-- 个人信息 -->
    <UserInfo :memberInfo="memberInfo" :isLogin="isLogin" :isAuth="isAuth" :pay="pay" :balance="balance"
      :orderTotal="orderTotal" />
    <!-- 菜单 -->
    <Menu :isLogin="isLogin" :checkMemberMpResult="checkMemberMpResult" />
    <!-- 工具 -->
    <Tool :isLogin="isLogin" />
  </view>
  <!-- 实名提示 -->
  <up-modal showCancelButton cancelText="3天内不再提示" confirmText="去认证" :show="showAuthModal" title="实名认证提示"
    @confirm="toAuth" @cancel="cancel">
    <view>检测到您还未进行实名认证,这会影响到后续您的提现操作,请及时完成实名认证</view>
  </up-modal>
  <MyTabbar name="我的"></MyTabbar>
</template>

<script lang="ts" setup>
import UserInfo from './components/userInfo.vue';
import Menu from './components/menu.vue';
import Tool from './components/tool.vue'
import { onShow } from '@dcloudio/uni-app';
import MyTabbar from '@/components/my-tabbar/my-tabbar.vue';
import { ref, reactive } from 'vue';
import { getWallet, getPayOrderAllMoney } from '@/api/mine/wallet'
import { checkMemberMp } from '@/api/mine/userInfo'
import { useMemberStore } from '@/stores/modules/member';
import type { PageParams } from '@/types/order';
import { getAcceptOrderPage } from '@/api/workerPage/order';
import { getAcceptOrderPage as getCustomerAcceptOrderPage } from '@/api/customerPage/order';
import { storeToRefs } from 'pinia';
const useMember = useMemberStore()
const { memberInfo, isLogin, userInfo, authInfo, isRealName, isIdNumber, isAuth } = storeToRefs(useMember)
onShow(() => {
  uni.hideTabBar()
  const role = uni.getStorageSync("role")

  if (isLogin.value) {
    // 更新认证信息
    useMember.updateAuthInfo()
    // 检查是否关注公众号
    checkMemberMpRes()

    // 如果是师傅 查询钱包余额和已接单数量
    if (role === 'worker') {
      getWalletInfo()
      getAcceptOrderList()
      // 检测是否实名认证提示
      checkAuth()
    }

    // 如果是客户 查询支出和已完成订单数量
    else if (role === 'customer') {
      getPayOrderAllMoneyInfo()
      getCustomerAcceptOrderList()
    }
  }
})
/**
 * 角色为师傅查询钱包余额和已接单数量
 */
// 查询余额
const balance = ref<number>(0)
const getWalletInfo = async () => {
  const res: any = await getWallet()
  balance.value = (res.data.balance ?? 0) + (res.data.completeBalance ?? 0)
}

// 查询订单总数
const orderTotal = ref<number>(0)
const acceptOrderParams = reactive<PageParams>({
  pageNo: 1,
  pageSize: 10,
  status: 23,
  workUserId: ""
})
const getAcceptOrderList = async () => {
  acceptOrderParams.workUserId = userInfo.value?.userId
  const res: any = await getAcceptOrderPage(acceptOrderParams)
  if (res.code == 0 && res.data) {
    orderTotal.value = res.data.total
  }
}
/**
 * 角色为客户 查询支出和已完成订单数量
 */

// 查询支出
const pay = ref<number>(0)
const getPayOrderAllMoneyInfo = async () => {
  const res: any = await getPayOrderAllMoney()
  pay.value = res.data ?? 0
}

// 查询已完成订单
const customerAcceptOrderParams = reactive<PageParams>({
  pageNo: 1,
  pageSize: 10,
  nodeStatus: 3,
  customUserId: ""
})
const getCustomerAcceptOrderList = async () => {
  customerAcceptOrderParams.customUserId = userInfo.value?.userId
  const res: any = await getCustomerAcceptOrderPage(customerAcceptOrderParams)
  if (res.code == 0 && res.data) {
    orderTotal.value = res.data.total
  }
}
// 查询是否关注公众号
const checkMemberMpResult = ref(false)
const checkMemberMpRes = async () => {
  const res: any = await checkMemberMp()
  if (res.code == 0) {
    checkMemberMpResult.value = res.data
  } else {
    checkMemberMpResult.value = false
  }
}
/**
 * 实名认证提示 如果师傅已填写真实姓名,身份证号,则提示补充实名认证(上传身份证照片和银行卡信息)
 */
const showAuthModal = ref(false)
const checkAuth = () => {
  const authModalWaitTime = uni.getStorageSync("authModalWaitTime") ?? 0
  // 已经选择3天不再提示
  if ((new Date().getTime() - authModalWaitTime) < (1000 * 60 * 60 * 24 * 3)) return
  // 未填写真实姓名,身份证号
  if (!isRealName.value || !isIdNumber.value) return
  // 已认证 或者审核中
  if (authInfo.value?.status == 0 || authInfo.value?.status == 1) return
  // 弹框提示
  showAuthModal.value = true
}
// 3天内不再提示
const cancel = () => {
  let authModalWaitTime = new Date().getTime()
  uni.setStorageSync("authModalWaitTime", authModalWaitTime)
  showAuthModal.value = false
}
const toAuth = () => {
  showAuthModal.value = false
  uni.navigateTo({
    url: '/pages/mine/authInfo/authInfo'
  })
}
</script>

<style lang="scss" scoped>
.mine {
  min-height: 100vh;
  overflow: hidden;
  background: #f8f8f8;
}
</style>